package com.ldb.sharedpreferencesstudy;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private EditText mEditTextNum;  //账号文本框
    private EditText mEditTextPassword; //密码文本框
    private Button mButtonLogin;     //登录按钮
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mEditTextNum=findViewById(R.id.et_number);
        mEditTextPassword=findViewById(R.id.et_password);

        //获取原本保存的账号和密码
        Map<String,String> userInfo = Utils.getUserInfo(this);

        /*若原先保存的账号和密码存在，则将其恢复到界面文本框中*/
        if(userInfo!=null){
            mEditTextNum.setText(userInfo.get("userName"));
            mEditTextPassword.setText(userInfo.get("password"));
        }
        mButtonLogin = findViewById(R.id.btn_login);
        /*为登录按钮设置监听器*/
        mButtonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String number = mEditTextNum.getText().toString().trim();
                String password = mEditTextPassword.getText().toString().trim();
                /*账号文本框为空*/
                if(number.isEmpty()){
                    Toast.makeText(MainActivity.this,"请输入QQ号码",Toast.LENGTH_SHORT).show();
                    return;
                }
                /*密码文本框为空*/
                if(password.isEmpty()){
                    Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                /*保存当前账号和密码*/
                boolean isSaveSuccess = Utils.saveUserInfo(MainActivity.this,number,password);
                if(isSaveSuccess){
                    Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this,"保存失败",Toast.LENGTH_SHORT).show();
                }
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,LoginSuccess.class);
                startActivity(intent);
            }
        });
        

    }
}